<template>
	<view>
      <!-- 自定义搜索组件 -->
      <my-search @click="btnclick"></my-search>
      <!-- <my-search :bgcolor="'pink'" :radius="6"></my-search> -->
		<view class="scroll-view-container" >
      <!-- 左侧滑动区域 -->
		<scroll-view class="scroll-left" scroll-y="true" :style="{height:vh+ 'px'}">
      
      <block v-for="(item , i) in cateList" :key="i">
        <!-- <view class="left-scroll-item" :class="i === active ? 'active' : ''" >{{item.cat_name}}</view> -->
        <view :class="[ 'left-scroll-item' , i === active? 'active' : '']" @click="activeChange(i)">{{item.cat_name}}</view>
        </block>
    </scroll-view>
    <!-- 右侧滑动区域 -->
    <scroll-view class="scoll-right" scroll-y="true" :style="{height:vh+ 'px'}" :scroll-top="scrollTop">
      <view class="right-scroll-item" v-for="(item2 , i) in cateLeval2" :key="i" >
        <!-- 标题 -->
        <view class="cate-leval2-title">
          / {{item2.cat_name}} /
        </view>
        <!-- 商品列表 -->
        <view class="cate-leval3-list">
          <view class="cate-leval3-item" v-for="(item3 , ind) in item2.children" :key="ind" @click="toShopList(item3)">
            <!-- 商品图片 -->
            <image :src="item3.cat_icon" mode="widthFix" ></image>
            <!-- 商品名称 -->
            <text>{{item3.cat_name}}</text>
          </view>
        </view>
        
      </view>
     
    </scroll-view>
		</view>
	</view>
</template>

<script>
  import badgeMix from '../../mixin-tabbar-badge/tabbar_badge.js'
	export default {
     mixins:[badgeMix],
		data() {
			return {
				vh:0,
        //分类列表
        cateList:[],
        active:0,
        cateLeval2:[],
        //滚动条距离顶部距离
        scrollTop : 0
			};
		},
    onLoad() {
     const sysInfo= uni.getSystemInfoSync()
     // console.log(sysInfo);
     this.vh = sysInfo.windowHeight - 50
     //获取分类列表
     this.getCateList()
    },
    methods:{
     async getCateList(){
      const res = await uni.$http.get("/categories")
     
      if(res.data.meta.status !== 200){
       return uni.showMsg('获取数据失败' , 1500)
      }
      this.cateList = res.data.message
      uni.showMsg('获取数据成功')
       // console.log(this.cateList);
       this.cateLeval2 = res.data.message[0].children
      },
      activeChange(i){
        this.active = i
        this.cateLeval2 = this.cateList[i].children
        //scrolltop属性只有在两次不同时才会触发
        //仅仅给scrollTop赋值为 0 不生效
       this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      toShopList(item){
        // console.log(item);
        uni.navigateTo({
          url:'/subpkg/goods-list/goods-list?cid=' + item.cat_id
        })
      },
      //组件点击事件
      btnclick(){
        console.log('ok');
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">
.scroll-view-container {
  display: flex;
  .scroll-left {
    width: 120px;
  }
  .left-scroll-item {
    background-color: #F7F7F7;
    line-height: 60px;
    text-align :center ;
    font-size: 12px;
    &.active {
      background-color: #fff;
      position: relative;
      &::before {
        content: '';
        display: block;
        width: 3px;
        height: 30px ;
        background-color: #C00000;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
}
.cate-leval2-title {
  text-align: center;
  font-size: 12px;
  font-weight: bold;
  padding: 15px 0;
}
.cate-leval3-list {
  display: flex;
  flex-wrap: wrap;
  .cate-leval3-item {
    width: 33.33%;
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    image {
      width: 60px;
      height: 60px;
    }
    text {
      font-size: 12px;
    }
  }
}
</style>
